package com.edu.exam.entity.grading;

import com.edu.exam.enums.common.WorkflowStatus;
import com.edu.exam.enums.common.TaskType;
import com.edu.exam.enums.quality.monitoring.PriorityLevel;
import com.edu.exam.enums.common.ProcessResult;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 协作会话实体类
 * 所属模块：B-06 成绩管理模块
 *
 * 根据E-04-05 网上批阅子模块设计实现
 * 对应collaboration_session数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class CollaborationSession {

    @Id
    @Column(name = "session_id", length = 64)
    private String sessionId;

    /**
     * 图像ID
     */
    @Column(name = "image_id", nullable = false, length = 64)
    private String imageId;

    /**
     * 发起人ID
     */
    @Column(name = "initiator_id", nullable = false, length = 64)
    private String initiatorId;

    /**
     * 会话状态
     * 映射到WorkflowStatus枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "session_status", nullable = false, length = 32)
    private WorkflowStatus sessionStatus;

    /**
     * 协作类型
     * TODO: 需要创建专用的CollaborationType枚举
     */
    @Column(name = "collaboration_type", nullable = false, length = 32)
    private String collaborationType;

    /**
     * 最大参与人数
     */
    @Column(name = "max_participants", nullable = false)
    private Integer maxParticipants;

    /**
     * 开始时间
     */
    @Column(name = "start_time", nullable = false)
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 最终决策（JSON格式）
     */
    @Column(name = "final_decision", length = 2000, columnDefinition = "TEXT")
    private String finalDecision;

    /**
     * 冲突解决（JSON格式）
     */
    @Column(name = "conflict_resolution", length = 2000, columnDefinition = "TEXT")
    private String conflictResolution;

    /**
     * 会话标题
     */
    @Column(name = "session_title", length = 200)
    private String sessionTitle;

    /**
     * 会话描述
     */
    @Column(name = "session_description", length = 1000, columnDefinition = "TEXT")
    private String sessionDescription;

    /**
     * 会话目标
     */
    @Column(name = "session_objective", length = 1000, columnDefinition = "TEXT")
    private String sessionObjective;

    /**
     * 会话议程（JSON格式）
     */
    @Column(name = "session_agenda", length = 2000, columnDefinition = "TEXT")
    private String sessionAgenda;

    /**
     * 预计持续时间（分钟）
     */
    @Column(name = "estimated_duration_minutes")
    private Integer estimatedDurationMinutes;

    /**
     * 实际持续时间（分钟）
     */
    @Column(name = "actual_duration_minutes")
    private Integer actualDurationMinutes;

    /**
     * 优先级
     * 映射到PriorityLevel枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "priority", length = 16)
    private PriorityLevel priority;

    /**
     * 敏感级别：PUBLIC（公开）、INTERNAL（内部）、CONFIDENTIAL（机密）、SECRET（秘密）
     * TODO: 需要创建专用的SensitivityLevel枚举
     */
    @Column(name = "sensitivity_level", length = 16)
    private String sensitivityLevel;

    /**
     * 是否允许匿名参与
     */
    @Column(name = "allow_anonymous_participation")
    private Boolean allowAnonymousParticipation;

    /**
     * 是否需要录音
     */
    @Column(name = "requires_recording")
    private Boolean requiresRecording;

    /**
     * 是否自动保存
     */
    @Column(name = "auto_save_enabled")
    private Boolean autoSaveEnabled;

    /**
     * 自动保存间隔（秒）
     */
    @Column(name = "auto_save_interval_seconds")
    private Integer autoSaveIntervalSeconds;

    /**
     * 协作规则（JSON格式）
     */
    @Column(name = "collaboration_rules", length = 2000, columnDefinition = "TEXT")
    private String collaborationRules;

    /**
     * 投票配置（JSON格式）
     */
    @Column(name = "voting_config", length = 1000, columnDefinition = "TEXT")
    private String votingConfig;

    /**
     * 决策机制：CONSENSUS（共识一致）、MAJORITY_VOTE（多数投票）、EXPERT_DECISION（专家决定）、INITIATOR_DECISION（发起人决定）
     * TODO: 需要创建专用的DecisionMechanism枚举
     */
    @Column(name = "decision_mechanism", length = 32)
    private String decisionMechanism;

    /**
     * 决策所需最低人数
     */
    @Column(name = "min_decision_participants")
    private Integer minDecisionParticipants;

    /**
     * 决策所需最低比例（0-1）
     */
    @Column(name = "min_decision_ratio", precision = 3, scale = 2)
    private Double minDecisionRatio;

    /**
     * 冲突检测级别：NONE（无检测）、LOW（低）、MEDIUM（中）、HIGH（高）
     * TODO: 需要创建专用的ConflictDetectionLevel枚举
     */
    @Column(name = "conflict_detection_level", length = 16)
    private String conflictDetectionLevel;

    /**
     * 质量要求：STANDARD（标准）、HIGH（高）、EXPERT（专家级）
     * TODO: 需要创建专用的QualityRequirement枚举
     */
    @Column(name = "quality_requirement", length = 16)
    private String qualityRequirement;

    /**
     * 专家评审要求：NONE（无需）、OPTIONAL（可选）、REQUIRED（必须）
     * TODO: 需要创建专用的ExpertReviewRequirement枚举
     */
    @Column(name = "expert_review_requirement", length = 16)
    private String expertReviewRequirement;

    /**
     * 会话结果
     * 映射到ProcessResult枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "session_outcome", length = 32)
    private ProcessResult sessionOutcome;

    /**
     * 参与者满意度（1-5）
     */
    @Column(name = "participant_satisfaction_score", precision = 3, scale = 2)
    private Double participantSatisfactionScore;

    /**
     * 协作质量分数（0-100）
     */
    @Column(name = "collaboration_quality_score", precision = 5, scale = 2)
    private Double collaborationQualityScore;

    /**
     * 效率评分（0-100）
     */
    @Column(name = "efficiency_score", precision = 5, scale = 2)
    private Double efficiencyScore;

    /**
     * 会话日志（JSON格式）
     */
    @Column(name = "session_log", length = 5000, columnDefinition = "TEXT")
    private String sessionLog;

    /**
     * 关键决策点（JSON格式）
     */
    @Column(name = "key_decision_points", length = 2000, columnDefinition = "TEXT")
    private String keyDecisionPoints;

    /**
     * 分歧记录（JSON格式）
     */
    @Column(name = "disagreement_records", length = 3000, columnDefinition = "TEXT")
    private String disagreementRecords;

    /**
     * 改进建议（JSON格式）
     */
    @Column(name = "improvement_suggestions", length = 2000, columnDefinition = "TEXT")
    private String improvementSuggestions;

    /**
     * 会话标签
     */
    @Column(name = "session_tags", length = 500)
    private String sessionTags;

    /**
     * 关联的后续任务ID列表（JSON格式）
     */
    @Column(name = "follow_up_task_ids", length = 1000, columnDefinition = "TEXT")
    private String followUpTaskIds;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 版本号（用于乐观锁）
     */
    @Version
    @Column(name = "version", nullable = false)
    private Integer version;

    // ================================
    // JPA 关联关系
    // ================================

    /**
     * 关联的批阅图像
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "image_id", insertable = false, updatable = false)
    private GradingImage gradingImage;

    /**
     * 关联的协作参与者列表
     */
    @OneToMany(mappedBy = "collaborationSession", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<CollaborationParticipant> participants;

    // ================================
    // 枚举已迁移
    // ================================
    // 嵌入枚举已迁移到通用枚举包或String + TODO标记
}